package com.example.messagequeue.demo;

import com.example.messagequeue.commom.Consumer;
import com.example.messagequeue.commom.MqException;
import com.example.messagequeue.mqclient.Channel;
import com.example.messagequeue.mqclient.Connection;
import com.example.messagequeue.mqclient.ConnectionFactory;
import com.example.messagequeue.mqserver.core.BasicProperties;
import com.example.messagequeue.mqserver.core.ExchangeType;

import java.io.IOException;

public class DempConsumer {
    public static void main(String[] args) throws IOException, InterruptedException, MqException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPort(9090);
        factory.setHost("127.0.0.1");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT, true, false, null);
        channel.queueDeclare("testQueue", true, false, false, null);
        channel.basicConsume("testQueue", true, new Consumer() {
            @Override
            public void handleDelivery(String consumeTag, BasicProperties basicProperties, byte[] body) throws IOException, MqException {
                System.out.println("[消费数据] 开始！");
                System.out.println("consumeTag = " + consumeTag);
                System.out.println("basicProperties = " + basicProperties);
                String bodyString = new String(body, 0, body.length);
                System.out.println("body = " + bodyString);
                System.out.println("[消费数据] 结束！");
            }
        });

        while (true){
            Thread.sleep(500);
        }

    }
}
